package com.itheima.mall.api.cms;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.mapper.cms.SubjectMapper;
import com.itheima.mall.pojo.cms.Subject;
import com.itheima.mall.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SubjectServiceApiImpl implements SubjectServiceApi {
    @Autowired
    private SubjectMapper subjectMapper;

    @Override
    public PageResult findPageByName(String keyword, Integer pageNum, Integer pageSize) {
        Page pageCondition = new Page(pageNum,pageSize);
        LambdaQueryWrapper<Subject> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ObjectUtil.isNotNull(keyword),Subject::getCategoryName,keyword);
        Page pageR = subjectMapper.selectPage(pageCondition, lqw);
        PageResult pageResult = new PageResult();
        pageResult.setList(pageR.getRecords());
        pageResult.setPageSize(pageSize);
        pageResult.setPageNum(pageNum);
        pageResult.setTotalPage(Convert.toInt(pageR.getPages()));
        pageResult.setTotal(Convert.toInt(pageR.getTotal()));
        return pageResult;
    }




    @Override
    public List<Subject> listAll() {
        return subjectMapper.selectList(null);
    }
}
